﻿using Cart.Bll;
using Cart.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;
using System.Xml.Linq;

namespace Cart.Api.Controllers
{
    public class GoodsController : ApiController
    {
        // GET: api/Goods
        //列表
        [HttpGet]
        [Route("Api/Goods/Page")]
        public ResultPageList<GoodDto> Get(int page, int limit, string name = "")
        {
            int count = 0;
            List<Goods> data = new GoodsBll().GetGoodsByPage(name, page, limit, out count);

            var list = from g in data
                           // 商品信息
                       select new GoodDto
                       {
                           GID = g.gId,
                           Name = g.name,
                           Content = g.content,
                           Images = g.images,
                           Sales = g.sales,
                           Unit = g.unit,
                           Use_Spec = g.use_spec,
                           Use_Property = g.use_property,
                           GTID = g.gtId,
                           TypeName = g.GoodsType.name,
                           Price = g.price,
                           GoodPropertyKey = from gpk in g.GoodsPropertyKey
                                                 //商品的属性 温度 糖度 配料
                                             select new GoodPropertyKeyDto
                                             {
                                                 GPKID = gpk.gpkId,
                                                 Name = gpk.GoodsSpecKey.name,
                                                 GSKID = gpk.gskid,
                                                 GoodPropertyValue = from gpv in gpk.GoodsPropertyValue
                                                                         //属性值 
                                                                     select new GoodPropertyValueDto
                                                                     {
                                                                         GPVID = gpv.gpvId,
                                                                         GSVID = gpv.gsvId,
                                                                         Code = gpv.GoodsSpecValue.code,
                                                                         Value = gpv.GoodsSpecValue.value,
                                                                         Is_Default = gpv.is_default
                                                                     }
                                             }
                       };

            ResultPageList<GoodDto> result = new ResultPageList<GoodDto>();
            result.code = 0;
            result.msg = "ok";
            result.count = count;
            result.data = list.ToList();

            return result;
        }

        // GET: api/Goods/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/Goods
        public Result Post([FromBody] GoodEditDto value)
        {

            var good = new Goods();
            good.gId = 0;
            good.name = value.Name;
            good.price = value.Price;
            good.unit = value.Unit;
            good.gtId = value.GTID;
            good.use_property = value.Use_Property;
            good.use_spec = false;
            good.content = value.Content;
            good.sales = 0;
            good.images = value.Images;
            good.sort = value.Sort;
            good.GoodsPropertyKey=new List<GoodsPropertyKey>();

            foreach (GoodEditPropertyKeyDto it  in value.GoodPropertyKey)
            {
                GoodsPropertyKey key = new GoodsPropertyKey() {
                    gpkId = 0,
                    gskid = it.GSKID,
                    GoodsPropertyValue = new List<GoodsPropertyValue>() { }

                };
                good.GoodsPropertyKey.Add(key);
                int i=0;
                foreach (int gsvid in it.GoodPropertyValue)
                {
                     
                    key.GoodsPropertyValue.Add(new GoodsPropertyValue()
                    {
                        gpvId = 0,
                        gsvId = gsvid,
                        is_default = (i == 0),
                    });
                    i++;
                }
            }

            bool bol = new GoodsBll().Add(good);

            Result re = new Result();
            re.code = bol ? 1 : 0;
            re.msg = bol ? "添加成功！" : "添加失败！";

            return re;
        }

        // PUT: api/Goods/5
        public Result Put([FromBody] GoodEditDto value)
        {
            var good = new Goods();
            good.gId = value.GID;
            good.name = value.Name;
            good.price = value.Price;
            good.unit = value.Unit;
            good.gtId = value.GTID;
            good.use_property = value.Use_Property;
            good.use_spec = false;
            good.content = value.Content;
            good.sales = 0;
            good.images = value.Images;
            good.sort = value.Sort;
            good.GoodsPropertyKey = new List<GoodsPropertyKey>() { };
            foreach (GoodEditPropertyKeyDto gpk in value.GoodPropertyKey)
            {
                GoodsPropertyKey key = new GoodsPropertyKey()
                {
                    gpkId = 0,
                    gId = good.gId,
                    gskid = gpk.GSKID,
                    GoodsPropertyValue = new List<GoodsPropertyValue>() { }
                };

                good.GoodsPropertyKey.Add(key);

                int i = 0;
                //商品属性值 values
                foreach (int gsvid in gpk.GoodPropertyValue)
                {
                    key.GoodsPropertyValue.Add(new GoodsPropertyValue()
                    {
                        gpvId = 0,
                        gsvId = gsvid,
                        is_default = i == 0,
                    });
                    i++;
                }
            }

            //修改
            bool bol = new GoodsBll().UpdateGood(good);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "修改成功！" : "修改失败！";

            return result;
        }

        // DELETE: api/Goods/5
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">商品编号</param>
        public Result Delete(int id)
        {
            GoodsBll gbl = new GoodsBll();
            Result re = new Result();
            var bo = gbl.DeleteGood(id);
            re.code = bo ? 1 : 0;
            re.msg = bo ? "ok" : "error";
            return re;
        }
        /// <summary>
        /// 删除商品集合
        /// </summary>
        /// <param name="ids">商品编号</param>
        /// <returns></returns>
        [Route("Api/Goods/delbatch")]
        public Result DeleteBatch(int[] ids) 
        {
            GoodsBll gbl = new GoodsBll();
            Result re = new Result();
            var bo = gbl.DeleteBatchGood(ids);
            re.code = bo ? 1 : 0;
            re.msg = bo ? "ok" : "error";
            return re;
        }


    }
}
